﻿using Assets.Core.Common.Web;
using Assets.Core.Jwt;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Nucleic.Core.DoMain;
using Nucleic.Core.Utils;
using Nucleic.Web.Dto.InHospital;
using Nucleic.Web.Models.InHospital;
using SqlSugar;

namespace Nucleic.Web.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class InHospitalController : ControllerBase
    {
        public UserInfo? UserInfo
        {
            get
            {
                var token = HttpContext.Request.Headers["token"].ToString();
                if (string.IsNullOrEmpty(token))
                {
                    return null;
                }
                var result = JwtHelp.CheckToken(token);
                if (result.Success)
                {
                    return result.Data;
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 操作员登录
        /// </summary>
        /// <param name="oper_id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Authenticate")]
        public Result Authenticate(string oper_id)
        {
            var oper = SqlSugarFactory.Instance.Queryable<@operator>()
                  .Where(t1 => t1.oper_id == oper_id)
                  .First();
            if (oper == null)
            {
                return Result.fail("用户名不存在");
            }
            if (oper.status != "0")
            {
                return Result.fail("用户禁止登录");

            }
         
            //生成toen
            var payload = new Dictionary<string, object>
                {
                    { "id",oper.oper_id },
                    { "name",oper.name },
                    { "iatTime", DateTime.Now },
                    //{ "expTime", DateTime.Now.AddHours(1)},
                    { "expTime", DateTime.Now.AddDays(1)},
                };
      
            var token = JwtHelp.SetJwtEncode(payload);

            //var tokena= Request.Headers["token"].FirstOrDefault();
            //var c = JwtHelp.CheckToken(token);

            return Result.success(token);
        }
        /// <summary>
        /// 查询住院患者信息
        /// </summary>
        /// <param name="idnum"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Query")]
        public Result Query(string idnum)
        {
            var idnu = new SugarParameter("@idnum", idnum);
            var patient = SqlSugarFactory.Instance.Ado.UseStoredProcedure().SqlQuery<string>("usp_inhos_patinfo", idnu).FirstOrDefault();
            if (string.IsNullOrEmpty(patient))
            {
                return Result.fail("信息未找到");
            }
            return Result.success(patient);
        }
        /// <summary>
        /// 保存人员信息及订单
        /// </summary>
        /// <param name="idnum"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Book")]
        public Result Book(BookInput input)
        {
            var pat_idnum = new SugarParameter("@pat_idnum", input.pat_idnum);
            var rela_idnum = new SugarParameter("@rela_idnum", input.rela_idnum);
            var rela_name = new SugarParameter("@rela_name", input.rela_name);
            var rela_sex = new SugarParameter("@rela_sex", input.rela_sex);
            var rela_addr = new SugarParameter("@rela_addr", input.rela_addr);
            var rela_tel = new SugarParameter("@rela_tel", input.rela_tel);

            var patient = SqlSugarFactory.Instance.Ado.UseStoredProcedure()
                .SqlQuery<string>("usp_inhos_patinfo_ins", pat_idnum, rela_idnum, rela_name, rela_sex, rela_addr, rela_tel).FirstOrDefault();
            if (string.IsNullOrEmpty(patient))
            {
                return Result.fail("失败1");
            }
            if (patient=="T")
            {
                return Result.success();
            }
            else
            {
                return Result.fail("失败");
            }
        }
        /// <summary>
        /// 查询样本管状态
        /// </summary>
        /// <param name="qrcode">样本管的条码</param>
        /// <returns></returns>
        [HttpPost]
        [Route("SampleStatus")]
        public Result SampleStatus(string qrcode)
        {
            var qr = new SugarParameter("@qrcode", qrcode);
            var oper_id = new SugarParameter("@oper_id", UserInfo==null?"": UserInfo.id);

            var sample_id = SqlSugarFactory.Instance.Ado.UseStoredProcedure()
                .SqlQuery<usp_sample_statusDto>("usp_sample_status", qr,oper_id).FirstOrDefault();
            if (sample_id==null)
            {
                return Result.fail("不能使用1");
            }
            if (sample_id.sample_id == 0)
            {
                return Result.fail("不能使用");
            }
            else
            {
                return Result.success(sample_id);
            }
        }
        /// <summary>
        /// 扫码预约吗存储
        /// </summary>
        /// <param name="sample_id"></param>
        /// <param name="orders_id">查询全部的话，传空</param>
        /// <returns></returns>
        [HttpPost]
        [Route("samplecollectinpat")]
        public Result<List<sample_collect_dto>> sample_collect_inpat(string sample_id,string? orders_id)
        {
                var ss = new SugarParameter("@sample_id", sample_id);
                var order = new SugarParameter("@orders_id", orders_id??"");
                var items = SqlSugarFactory.Instance.Ado.UseStoredProcedure()
                  .SqlQuery<sample_collect_dto>("usp_sample_collect_inpat", ss, order);
                return Result<List<sample_collect_dto>>.success(items);
            
        }
        /// <summary>
        /// 封管
        /// </summary>
        /// <param name="sample_id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("sample_end")]
        public Result sample_end(int sample_id)
        {
           var sa= SqlSugarFactory.Instance.Queryable<sample_info>().First(i => i.sample_id == sample_id);
            if (sa==null)
            {
                return Result.fail("样本号不存在");
            }
            sa.status = "1";
            sa.end_time = DateTime.Now;
            SqlSugarFactory.Instance.Updateable(sa).ExecuteCommand();

            return Result.success();
        }
    }
}
